<?php

namespace Common\Model;

/**
 * 订单入住人记录表
 * @modify trainyao 2016-01-06 创建
 * @index orderinfo_id (`isdelete`, `orderinfo_id`)
 */
Class OrderUserModel extends \Common\Model\BaseModel {

    protected $trueTableName = 'tb_order_user';

    protected function _before_update(&$data, $options) {
        // parent::_before_update($data, $options);
    }

    /**
     * 添加订单入住人
     * @modify songanwei 2016-01-06
     * @modify trianyao  2016-04-08  修改为增加多条
     * @param array $infoArray        添加多条信息数组
     * @return mixin 成功返回addAll的ID
     */
    public function addInfo($infoArray) {
        $result = $this->addAll($infoArray);
        return empty($result) ? false : $result;
    }

    /**
     * 更新订单用户数据
     * @modify songanwei 2016-01-11
     * @modify trainyao 2016-01-15     修改为可以修改部分数据
     * @modify wenhui 2017-05-04 增加区号
     * @param inteeger $orderUserId  订单用户ID
     * @param integer $orderId 订单ID
     * @param integer $userId 用户ID
     * @param string $mobile 手机号
     * @param string $name 入住人名称
     * @param string $identity 身份证
     * @param string $areaCode 区号
     * @return type
     */
    public function saveInfo($orderUserId, $orderId, $userId = null, $mobile = null, $name, $identity = null, $areaCode = null, $idsex = null, $idnation = null, $idaddress = null) {
        $options = array();

        if (!empty($orderId)) {
            $options['orderinfo_id'] = $orderId;
        }
        if (!is_null($userId)) {
            $options['userlogin_id'] = $userId;
        }
        if (!is_null($mobile)) {
            $options['mobile'] = $mobile;
        }
        if (!empty($name)) {
            $options['name'] = $name;
        }
        if (!is_null($identity)) {
            $options['identity'] = $identity;
        }
        if (!is_null($areaCode)) {
            $options['areacode'] = $areaCode;
        }
        if (!is_null($idsex)) {
            $options['idsex'] = $idsex;
        }
        if (!is_null($idnation)) {
            $options['idnation'] = $idnation;
        }
        if (!is_null($idaddress)) {
            $options['idaddress'] = $idaddress;
        }

        $result = $this->where(array('id' => $orderUserId))->save($options);
        return $result === false ? false : true;
    }

    /**
     * 添加多条数据
     * @modify songanwei 2016-01-07
     * @param  $data
     * @return type
     */
    public function addInfos($data) {
        return $this->addAll($data);
    }

    /**
     * 根据id查询
     * @modify trainyao     2016-04-20 创建
     * @param mixin $orderUserId    orderUserId(s)
     * @return type
     */
    public function getInfo($orderUserId) {
        $option = array();
        if (is_array($orderUserId)) {
            $orderUserId = implode(',', $orderUserId);
        }
        if (strpos($orderUserId, ',') !== false) {
            $option['id'] = array('in', $orderUserId);
            return $this->where($option)->select();
        } else {
            $option['id'] = $orderUserId;
            return $this->where($option)->find();
        }
    }

    /**
     * 根据身份证查询
     * @modify wenhui     2017-07-3 创建
     * @param string $identity   身份证
     * @return date
     */
    public function getInfoByIdentity($identity, $orderInfoIdAll) {
        $option['orderinfo_id'] = array('in', $orderInfoIdAll);
        $option['identity'] = $identity;
        return $this->where($option)->order('id desc')->find();
    }

    /**
     * 根据身份证查询
     * @modify wenhui     2017-07-3 创建
     * @param string $identity   身份证
     * @return date
     */
    public function getInfoByMobile($mobile, $areaCode) {

        $option['mobile'] = $mobile;
        $option['areacode'] = $areaCode;
        return $this->where($option)->order('id desc')->find();
    }

    /**
     * 根据订单获取入住人
     * @modify trainyao 2016-01-06 创建
     * @param  mixin   $orderId   订单id(s)
     * @param  integer   $type    获取数据类型 1=筛选时的数据 2=获取导出表格时的数据
     * @return array              订单入住人信息数组
     */
    public function getInfoByOrder($orderId, $type = null) {
        $option = array();
        if (is_array($orderId)) {
            $orderId = implode(',', $orderId);
        }
        if (strpos($orderId, ',') !== false) {
            $option['orderinfo_id'] = array('in', $orderId);
        } else {
            $option['orderinfo_id'] = $orderId;
        }

        if(!is_null($type)){
            if ($type === 1) {
                $option['mobile'] = array('neq','');
            }

            $field = 'id,orderinfo_id,userlogin_id,areacode,mobile,name,identity,idsex,idnation,idaddress';
            $item = $this->field($field)->where($option)->order('id desc')->select();
            return empty($item) ? array() : $item;
        } else {
            $data = $this->where($option)->select();
        }
        
        return empty($data) ? array() : $data;
    }

    /**
     * 根据ID删除数据
     * @modify Amos 2015-09-25 创建
     * @modify trainyao 2016-01-04 增加根据多个id删除
     * @param mixin $ids
     * @return boolean 删除成功与否
     */
    public function deleteInfo($id) {
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') !== false) {
            $option['id'] = array('in', $id);
        } else {
            $option['id'] = $id;
        }
        $data = array(
            'isdelete' => IS_DELETE
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据用户获取入住人信息
     * @modify wenhui 2017-02-18 创建
     * @param  string   $userId
     * @return array              订单入住人信息数组
     */
    public function getInfoByUserId($userId) {
        $option['userlogin_id'] = $userId;

        $data = $this->where($option)->group('orderinfo_id')->select();
        return empty($data) ? array() : $data;
    }
}
